<?php require_once(dirname(__FILE__) . '/../../include/config.inc.php');
header('Content-type: application/json;charset=utf-8');
$dosql->Execute("SELECT `id` FROM `#@__order` WHERE  is_delete=0 ", 6);
$count_all = $dosql->GetTotalRow(6); //所有工单

$dosql->Execute("SELECT `id` FROM `#@__order` WHERE `jindu`=1  AND is_delete=0 ", 1);
$count1 = $dosql->GetTotalRow(1); //等待受理

$dosql->Execute("SELECT `id` FROM `#@__order` WHERE `jindu`=3  AND is_delete=0 ", 1);
$waiting = $dosql->GetTotalRow(1); //等待受理


//已接单为签到
$dosql->Execute("SELECT `id` FROM `#@__order` WHERE jindu='4'   AND is_delete=0 ", 88);
$rev = $dosql->GetTotalRow(88); //等待受理

//已接单已经签到
$dosql->Execute("SELECT `id` FROM `#@__order` WHERE jindu='4' and qiandao='true'  AND is_delete=0 ", 88);
$doing = $dosql->GetTotalRow(88); //等待受理

//受理中的工单
$dosql->Execute("SELECT `id` FROM `#@__order` WHERE jindu='5'   AND is_delete=0 and isstop=0 ", 99);
$ok = $dosql->GetTotalRow(99); //等待受理



$count = array('all' => $count_all,'apply'=>$count1, 'waiting' => $waiting,'rev' => $rev, 'doing' => $doing, 'ok' => $ok);

//地图
$map=[];
// 签到 接单 数据
$sql = "SELECT f.* ,j.dataname as jinduname,o.truename as truename , o.address as address, o.school as school,o.mobile as mobile,o.post_time as post_time,o.manageid as manageid,o.apply_device,o.id as orderid FROM `#@__order_log` as f
left join `#@__order`  as o on o.id=f.orderid  
left join `#@__cascadedata`  as j on j.datavalue=f.jindu  
WHERE  j.datagroup='jindu'  AND   o.is_delete=0  AND  f.latitude!='' AND  f.qiandao='true' and f.jindu='4'  AND o.jindu>0  AND o.jindu<5  ";

$dosql->Execute($sql,9);
while($row2 = $dosql->GetArray(9))
{
    $map_item['latlngs']=$row2['latitude'].",".$row2['longitude'];
    $map_item['lat']=$row2['latitude'];
    $map_item['lng']=$row2['longitude'];
    if($row2['qiandao']=='true'){
        $row2['jindu']='qian';
    }
    if($row2['jiedan']=='true'){
        $row2['jindu']='jie';
    }
    $map_item['jindu']=$row2['jindu'];

    //获取工程师
    $m_str="";
    if($row2['manageid']!=""){
        $m_manageid=explode(",",$row2['manageid']);
        foreach ($m_manageid as $key=>$val){
            $m_str.=" ".getManage($val);
        }
    }
    $map_item['manageids']=$m_str;

    $map_item['oder_info']="<h2 class='school_name'>".$row2['school']."</h2><br/>".$row2['truename']." ".$row2['mobile']."<br/>".$row2['address']."<br/>单号：".date("Ymd",$row2['post_time'])."-".$row2['orderid']."<br/>报修时间：".date("Y-m-d H:i:s",$row2['post_time'])."<br/>设备：".getRepairMode($row2['apply_device'])."<br/>进度：".$row2['jinduname']."<br/><br/><a href='javascript:;' class='btn btn-success'  onClick=dongdanshow('".$row2['orderid']."')>点击查看</a>";


    $map[]=$map_item;
}


// 工程 签到 接单 数据
$sql2 = "SELECT f.*,o.truename as truename , o.address as address, o.school as school,o.mobile as mobile,o.post_time as post_time,o.manageid as manageid,o.apply_device,o.id as orderid FROM (SELECT *  FROM `#@__project_log` where latitude!='' and  signIn='1'
  ORDER BY id DESC limit 1000)  f
left join `#@__project`  as o on o.id=f.orderid  
WHERE  o.is_delete=0   AND o.jindu>0  AND o.jindu<5  group by f.orderid  order by f.id ";

$dosql->Execute($sql2,9);
while($row2 = $dosql->GetArray(9))
{
    $map_item['latlngs']=$row2['latitude'].",".$row2['longitude'];
    $map_item['lat']=$row2['latitude'];
    $map_item['lng']=$row2['longitude'];
    if($row2['signIn']=='1'){
        $row2['jindu']='qian';
    }

    $map_item['jindu']=$row2['jindu'];

    //获取工程师
    $m_str="";
    if($row2['manageid']!=""){
        $m_manageid=explode(",",$row2['manageid']);
        foreach ($m_manageid as $key=>$val){
            $m_str.=" ".getManage($val);
        }
    }
    $map_item['manageids']=$m_str;

    $map_item['oder_info']="<h2 class='school_name'>".$row2['school']."</h2><br/>".$row2['truename']." ".$row2['mobile']."<br/>".$row2['address']."<br/>单号：".date("Ymd",$row2['post_time'])."-".$row2['orderid']."<br/>施工时间：".date("Y-m-d H:i:s",$row2['op_time'])."<br/>类型：".getRepairMode($row2['apply_device'])."<br/>进度：施工中<br/><br/><a href='javascript:;' class='btn btn-success'  onClick=projectshow('".$row2['orderid']."')>点击查看</a>";


    $map[]=$map_item;
}

//人员动态
//获取人员流水
$dongtai = "";
$today = date("Y-m-d", time());
$s_time = strtotime($today);
$sql = "select * from `#@__order_log`  where op_time>'$s_time'  order by id desc  ";
$dosql->Execute($sql, 6);
while ($row = $dosql->GetArray(6)) {

    if ($row['fankui'] == 'true' || $row['wanxiu'] == '1' || $row['jiedan'] == 'true' || $row['qiandao'] == 'true' || $row['zhuandan'] == 'true') {
        $dongtai .= date("m-d H:i", $row['op_time']) . "   <b style='color: red;'>". $row['op'] . "</b>    <b style='color: lightgreen;'>" . GetMembersName($row['manageid']) . "</b>  [" . $row['orderid'] . "] " . $row['address_name'] . "<br>";
    }


}






$json_json=[];

$json_json['errno']=0; //json状态为ok
$json_json['data']['map']=$map; //json状态为ok
$json_json['data']['count']=$count; //json状态为ok
$json_json['data']['dongtai']=$dongtai; //json状态为ok
$data = json_encode($json_json, JSON_UNESCAPED_UNICODE);
echo $data;



//获取前12个月月份
function getTimeAndCenters()
{
    //获取前6个月时间
    $beforeTime = [];
    for ($i = 0; $i < 12; $i++) {
        if ($i == 0) {
            $beforeTime[$i] = date('Y-m');
        } else {
            $base = strtotime(date('Y-m') . '-01 00:00:01');
            $t = "-" . $i . " month";
            $beforeTime[$i] = date('Y-m', strtotime($t, $base));
        }
    }

    return $beforeTime;

}

function getNextMonth($date){
    //获取指定日期下个月的第一天和最后一天
    $arr = getdate(strtotime($date));
    if ($arr['mon'] == 12) {
        $year = $arr['year'] + 1;
        $month = $arr['mon'] - 11;
        $day = $arr['mday'];
        if ($day < 10) {
            $mday = '0' . $day;
        } else {
            $mday = $day;
        }
        $firstday = $year . '-0' . $month . '-01';
        $lastday = $year . '-0' . $month . '-' . $mday;
    } else {
        $time = strtotime($date);
        $firstday = date('Y-m-01', strtotime(date('Y', $time) . '-' . (date('m', $time) + 1) . '-01'));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
    }
    $array = array('sdate' => $firstday, 'edate' => $lastday);
    return $array;
}

//获取工程师当月的积分
function getFeeThisMonth($mid){
    //获取所有的 记录合计
    global $dosql;
    $all_fee="0";
    $sql_order="SELECT * FROM   `#@__fee_log`   WHERE manageid='$mid'  AND  FROM_UNIXTIME(`op_time`,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') ";
    $dosql->Execute($sql_order,7);
    while($order=$dosql->GetArray(7)){
        if($order['op']=='add'){
            $all_fee+=$order['fee'];
        }elseif($order['op']=='move'){
            $all_fee-=$order['fee'];
        }
    }
    return $all_fee;
}

//获取 服务工程师
function getManage($mid){
    global $dosql;
    if($mid>0){
        $m=$dosql->GetOne("SELECT * FROM `#@__user` WHERE id='$mid' ORDER BY id desc");
        if(is_array($m) && isset($m['id']) ){
            return  $m['truename'];
        }
    }

}



/*
 * 函数说明：获取 多个工作人员 的名称
 *
 * @access  public
 * @param   $id   int  碎片ID
 * @param   $t    int  调用的内容 0为内容 1为标识名称 2为缩略图 3为跳转连接
 * @return  string     返回碎片缩略图地址
 */
function GetMembersName($ids = 0)
{
    global $dosql;
    if ($ids != "") {
        $men_names = "";
        $member = explode(",", $ids);
        if (count($member) > 1) {
            $i = 0;
            foreach ($member as $v) {
                $i++;
                if ($i < count($member)) {
                    $men_names .= getManage($v) . " ";
                } else {
                    $men_names .= getManage($v);
                }
            }
        } else {
            $men_names = getManage($ids);
        }
        return $men_names;
    } else {
        return "";
    }
}


//获取任务类型
function getRepairMode($mid){
    global $dosql;
    if($mid>0){
        $m=$dosql->GetOne("SELECT p.`classname`,t.`title` FROM `#@__task` as t left join `#@__tasktype` as p  on t.classid=p.id  WHERE t.id='$mid' ");
        if(is_array($m) && isset($m['title']) ){
            return $m['classname']."-".$m['title'];
        }
    }

}


?>

